const {ipcRenderer} = require("electron");
const fs = require("fs");
const readline = require("readline");
const utils = require("./utils");

function readFileToArr(fReadName,callback){
    var fRead = fs.createReadStream(fReadName);
    var objReadline = readline.createInterface({
        input:fRead
    });
    var arr = new Array();
    objReadline.on('line',function (line) {
        arr.push(line);
        //console.log('line:'+ line);
    });
    objReadline.on('close',function () {
       // console.log(arr);
        callback(arr);
        fRead.close();
    });
}

function closeHandling(arr) {
    console.log("closed")
    for(item of arr) {
        console.log(item)
    }
}

function splitLyr(arr) {
    console.log("aaaaa");
    let isChn = true;
    for(item of arr){
        if(!utils.isChinese(item)) {
            isChn = false;
            break;
        }
    }
    if(isChn) {
        utils.splitChn(arr);
    }else{
        utils.splitEn(arr);
    }
}

class Test {
    constructor() {
        this.btnSplitLyr = document.getElementById("btnSplitLyr");
        this.btnGenK = document.getElementById("btnGenK")
        this.addListener();
    }

    addListener() {
        this.btnSplitLyr.onclick = function() {
            // fs.readFile("package.json", function(err, data){
            //     if(err) return console.log(err);
            //     console.log(data.toString())
            // });
            readFileToArr("lyr.txt", splitLyr);
            // split lyrics algorithm
            
        }

        this.btnGenK.onclick = function() {
            readFileToArr("package.json", splitLyr);
        }
    }

}

new Test()